package com.mxpio.erp.asset.utils.excel;

/**
 * @Author : 吴盼盼
 * @Datetime : 2022/10/14 11:06
 * @Desc :
 * @Modor :  Modifytime:
 * @modDesc :
 */
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExcelUtilWrite {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelUtilWrite.class);

    public ExcelUtilWrite() {
    }

    public static void writeWithData(OutputStream outputStream, Excel excel) throws FileNotFoundException {
        List dataList = excel.getDataList();
        if (dataList != null && dataList.size() != 0) {
            ExcelWriter writer = null;

            try {
                Class clazz = dataList.get(0).getClass();
                ExcelHead excelHead = (ExcelHead)clazz.getAnnotation(ExcelHead.class);
                String sheetName = excelHead.sheetName();
                writer = EasyExcel.write(outputStream, clazz).build();
                WriteSheet writeSheet = EasyExcel.writerSheet(sheetName).build();
                writer.write(excel.getDataList(), writeSheet);
            } catch (Exception var16) {
                LOGGER.error("error", var16);
            } finally {
                try {
                    if (writer != null) {
                        writer.finish();
                    }

                    if (outputStream != null) {
                        outputStream.close();
                    }
                } catch (IOException var15) {
                    LOGGER.error("error", var15);
                }

            }

        }
    }
}